/* eslint-disable no-multi-assign */
/* eslint-disable camelcase */
/* eslint-disable react/no-unsafe */
import React from 'react';
/* eslint-disable no-undef */
import Taro, { getCurrentInstance, eventCenter } from '@tarojs/taro';
import ShowModal from './showModal';

class ModalContent extends React.Component {
  UNSAFE_componentWillMount() {
    const onShowEventId: string = this.$instance.router?.onShow || '';
    // 监听
    eventCenter.on(onShowEventId, this.setInstance);
  }

  componentDidMount() {
    Taro.action = wx.action = this.action;
    Taro.modal = wx.modal = this.modal;
  }

  componentWillUnmount() {
    const onShowEventId: string = this.$instance.router?.onShow || '';
    // 卸载
    eventCenter.off(onShowEventId, this.setInstance);
  }

  $instance = getCurrentInstance();

  action: any = null;

  modal: any = null;

  setInstance = () => {
    this.action && (Taro.action = wx.action = this.action);
    this.modal && (Taro.modal = wx.modal = this.modal);
  };

  render() {
    return (
      <>
        <ShowModal ref={(node) => (this.modal = node)} {...this.props} />
      </>
    );
  }
}

export default ModalContent;
